package leetcode.题库.数组.普通数组;

/**
 * description:
 *
 * @author 颜真明
 * @date 2024/3/20  17:30
 */
public class Solution169 {
    public static void main(String[] args) {
        int[] arr = new int[]{8,8,7,7,7};
        System.out.println(majorityElement(arr));
    }

    public static int majorityElement(int[] nums) {
        int count = 1;
        int res = nums[0];
        for (int i = 0; i < nums.length-1; i++) {
            if (nums[i+1] != res){
                if (count > 1) {
                    count--;
                } else {
                    res = nums[i+1];
                    count = 1;
                }
            } else {
                count++;
            }
        }
        return res;
    }

}
